//
//  DialogViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/2.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class DialogViewController: NSViewController {
    
    override func loadView() {
        let view = PopoverContentView.init(frame: NSMakeRect(0, 0, 300, 220))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_dark.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
    }
    
    
    func loadData() {
        self.textField.stringValue = Td.sh().lc.nickName
        self.avatorImg.text = Td.sh().lc.nickName
    }
    
    func setupUI() {
        self.view.addSubview(closeButtom)
        closeButtom.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.left.equalTo(6)
            make.width.height.equalTo(closeButtom.image?.size ?? CGSize.zero)
        }
        
        self.view.addSubview(avatorImg)
        avatorImg.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
            make.width.height.equalTo(70)
        }
        
        
        self.view.addSubview(nameLab)
        nameLab.snp.makeConstraints { (make) in
            make.width.equalTo(60)
            make.left.equalTo(10)
            make.top.equalTo(120)
            make.height.equalTo(16)
        }
        
        self.view.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.right.equalTo(-40)
            make.height.equalTo(20)
            make.centerY.equalTo(nameLab)
            make.left.equalTo(nameLab.snp.right).offset(10)
        }
        
        self.view.addSubview(confirmBtn)
        confirmBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
            make.width.equalTo(80)
            make.height.equalTo(20)
        }
        
    }
    
    lazy var closeButtom: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.image(name:"btn_close_normal") ?? NSImage.init()
        btn.hoverImage = NSImage.image(name:"btn_close_hover") ?? NSImage.init()
        btn.highlightImage = NSImage.image(name:"btn_close_pressed") ?? NSImage.init()
        btn.target = self
        btn.action = #selector(closeButtomAction)
        return btn
    }()
    
    lazy var confirmBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "确定"
        btn.zh_font = NSFont.systemFont(ofSize: 12)
        btn.zh_color = color_gray(255)
        btn.zh_backgroundColor = color_dark_buttom
        btn.hasBorder = true
        btn.zh_cornerRadius = 3
        btn.target = self
        btn.action = #selector(confirmBtnActon)
        return btn
    }()
    
    lazy var textField: NSTextField = {
        let view = NSTextField.init(frame: NSRect.zero)
        view.maximumNumberOfLines = 1
        view.wantsLayer = true
        view.backgroundColor = color_dark38
        view.font = NSFont.systemFont(ofSize: 14)
        view.textColor = color_gray(140)
        view.placeholderString = "请输入"
        view.cell?.lineBreakMode = .byTruncatingTail
        view.cell?.truncatesLastVisibleLine = true
        view.cell?.usesSingleLineMode = true
        view.isBordered = false
        view.layer?.borderWidth = 1
        view.layer?.borderColor = color_gray(90).cgColor
        return view
    }()
    
    lazy var nameLab: NSLabel = {
        let lab = NSLabel.label(title: "姓名:", textColor: color_gray(200), font: NSFont.systemFont(ofSize: 12))
        lab.alignment = .right
        return lab
    }()
    
    
    lazy var avatorImg: KImageView = {
        let imageV = KImageView.init(text: "...", image: nil )
        imageV.wantsLayer = true
        imageV.layer?.backgroundColor = color_random_rgba().cgColor
        imageV.layer?.cornerRadius = 6
        imageV.layer?.masksToBounds = true
        imageV.layer?.borderColor = color_gray(230).cgColor
        imageV.layer?.borderWidth = 0
        return imageV
    }()
    
    @objc func closeButtomAction() {
        self.view.window?.performClose(nil)
    }
    @objc func confirmBtnActon() {
        let name = self.textField.stringValue
        if name != "" {
            IMEngine.shared.updateNickName(name: name, succ: {
                UserInfoManager.shared.name = name
                MTNot.postModyName(name: name)
                IMcmd.shared.modifyName(name: name)
                AsPopover.shared.popover.close()
            }, fail: nil)
            
        }
        
    }
}
